package com.jinxin.platform.cdacscontrolmgr.service.impl;

import com.jinxin.platform.base.common.pojo.JsonResult;
import com.jinxin.platform.cdacscontrolmgr.bean.CdacscontrolmgrCarRecord;
import com.jinxin.platform.cdacscontrolmgr.bean.CdhymgtOwnVehicles;
import com.jinxin.platform.cdacscontrolmgr.bean.CdyardmgrCarInside;
import com.jinxin.platform.cdacscontrolmgr.common.Constant;
import com.jinxin.platform.cdacscontrolmgr.mapper.CdacscontrolmgrCarInfoMapper;
import com.jinxin.platform.cdacscontrolmgr.mapper.CdacscontrolmgrCarInsideMapper;
import com.jinxin.platform.cdacscontrolmgr.mapper.CdacscontrolmgrCarRecordMapper;
import com.jinxin.platform.cdacscontrolmgr.mapper.CdacscontrolmgrOwnVehiclesMapper;
import com.jinxin.platform.cdacscontrolmgr.service.ICdacscontrolkmgrCarRecordService;
import com.jinxin.platform.cdacscontrolmgr.utils.IdWorker;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;

/**
 * All rights Reserved, Designed By www.beonelot.com
 *
 * @version: V1.0
 * @Copyright: www.beonelot.com Inc. All rights reserved.
 * @Description：
 * @Date: 2021-09-06
 * @Author: Lingnan
 **/
@Service
@Slf4j
public class CdacscontrolkmgrCarRecordServiceImpl implements ICdacscontrolkmgrCarRecordService {
    @Autowired
    CdacscontrolmgrCarRecordMapper cdacscontrolmgrCarRecordMapper;
    @Resource
    private IdWorker idWorker;
    @Autowired
    private CdacscontrolmgrCarInsideMapper carInsideMapper;
    @Autowired
    private CdacscontrolmgrOwnVehiclesMapper ownVehiclesMapper;

    @Override
    public CdacscontrolmgrCarRecord selectOne(HashMap param) {
        return cdacscontrolmgrCarRecordMapper.selectOne(param);
    }

    @Override
    public List<CdacscontrolmgrCarRecord> list(HashMap param) {
        return cdacscontrolmgrCarRecordMapper.list(param);
    }

    @Override
    public List<CdacscontrolmgrCarRecord> listPage(HashMap param) {
        return cdacscontrolmgrCarRecordMapper.listPage(param);
    }

    @Override
    public int listPageCount(HashMap param) {
        return cdacscontrolmgrCarRecordMapper.listPageCount(param);
    }

    @Override
    public JsonResult delete(String id) {
        if(StringUtils.isEmpty(id)){
            return JsonResult.error("Id不存在");
        }
        int result = cdacscontrolmgrCarRecordMapper.deleteByKey(id);
        if(result > 0){
            return JsonResult.ok("删除成功","");
        }
        return JsonResult.error("删除失败");
    }

    @Override
    public JsonResult saveOrUpdate(CdacscontrolmgrCarRecord record,boolean isComeIn) {
        if (record == null){
            return JsonResult.error("参数错误，操作失败");
        }
        String id = record.getId();
        int count;
        String msg = "";
        if(StringUtils.isEmpty(id)){
            String newId = String.valueOf(idWorker.nextId());
            record.setId(newId);
            count = cdacscontrolmgrCarRecordMapper.insert(record);
            if(isComeIn){
                //新增场内车辆
                CdyardmgrCarInside carInside = new CdyardmgrCarInside();
                carInside.setId(newId);
                carInside.setCategory(record.getCategory());
                String carNo = record.getCarNo();
                carInside.setCarNo(carNo);
                carInside.setEnterTime(LocalDateTime.now());
                HashMap param = new HashMap();
                param.put("ownVehicle",carNo);
                param.put("isAuth", Constant.IS_AUTHED);
                CdhymgtOwnVehicles ownVehicle = ownVehiclesMapper.selectOne(param);
                if(ownVehicle != null){
                    carInside.setPhone(ownVehicle.getMemberAccount());
                }else {
                    carInside.setPhone("-");
                }
                carInside.setProjectId(record.getProjectId());
                carInside.setIsDel(Constant.IS_DEL_NO);
                carInsideMapper.insert(carInside);
            }
            msg = "新增成功";
        }else {
            count = cdacscontrolmgrCarRecordMapper.updateByKey(record);
            msg = "更新成功";
        }
        if(count > 0){
            return JsonResult.ok(msg,id);
        }else {
            return JsonResult.error("操作失败，请重试");
        }
    }

    @Override
    public List<String> listBarrierOptions(String projectId) {
        return cdacscontrolmgrCarRecordMapper.listBarrierOptions(projectId);
    }

    @Override
    public JsonResult queryRecentCarInfo(String projectId, String mac) {
        HashMap param = new HashMap();
        param.put("projectId",projectId);
        param.put("mac",mac);
        CdacscontrolmgrCarRecord carRecord = cdacscontrolmgrCarRecordMapper.selectOne(param);
        return JsonResult.ok("success",carRecord);
    }

    @Override
    public List<String> listCategory(String projectId) {
        return cdacscontrolmgrCarRecordMapper.listCategory(projectId);
    }
}
